#include "GameCC.h"

Player::Player(char* Name, int Total, int Correct)
{
   strcpy(strName, Name);
   nTotal = Total;
   nCorrect = Correct;
}

void Player::save(void)
{
	char str[MAX_ENTRY];
	sprintf(str, "%d|%d", nCorrect, nTotal);
	WritePrivateProfileString("Players", strName, str, GetIniFileName());
}

Player* PlayersControlCenter::add(char* Name, int Total, int Correct)
{
   Player* NewPlayer; 
   NewPlayer = new Player(Name, Total, Correct);
   Players.add((void*)NewPlayer);
   return NewPlayer;
}

void PlayersControlCenter::load(void)
{
	char buf[MAX_BUFFER];
	char *name;
	int total, correct;
	
	GetPrivateProfileSection("Players", buf, sizeof(buf), GetIniFileName());

	clear();
	for (char* pName = buf; pName && *pName; pName = NextToken(pName, '\0'))
	{
		char entry[MAX_ENTRY];
		
		strcpy(entry, pName);
		name = strtok(entry, DELIMITERS);
		total = atoi(strtok(NULL, DELIMITERS));
		correct = atoi(strtok(NULL, DELIMITERS));
		add(name, total, correct);
	}
}

void PlayersControlCenter::printScores(void)
{
   ClearScreen();
	for (int i = 0; i < Players.getCount(); i++)
	{
		Player* player = (Player*) Players.item(i);
		printf("%s: %d / %d\n", player->strName, player->nCorrect, player->nTotal);
	}
   WaitForKey(PRESS_ANY_KEY);
}
/*
void PlayersControlCenter::save(void)
{
//   GetPrivateProfileSection("Players", buf, sizeof(buf), GetIniFileName()); ## delete existing [Palyers] section here

	for (int i = 0; i < Players.getCount(); i++)
	{
		Player* player = (Player*)Players.item(i);
		char str[MAX_ENTRY];
		sprintf(str, "%d|%d", player->nCorrect, player->nTotal);
		WritePrivateProfileString("Players", player->strName, str, GetIniFileName());
	}
}
*/